package me.jessyan.mvparms.demo.mvp.model.logic;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.widget.ImageView;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;


/**
 * @author Ryan Tang
 *
 */
public final class EncodingHandler {
	private static final int BLACK = 0xff000000;
	
	public static Bitmap createQRCode(String str, int widthAndHeight)  {

		Bitmap bitmap = null;
		return bitmap;
	}

	/**
	 * 把batmap 转file
	 * @param bitmap
	 * @param filepath
	 */
	public static File saveBitmapFile(Bitmap bitmap, String filepath, ImageView img){
		File file=new File(filepath,"ticket.png");//将要保存图片的路径
		if (file.exists()) {
			file.delete();
		}
		FileOutputStream stream = null;
		boolean flag = false;
		try {
			stream = new FileOutputStream(file);
			flag = bitmap.compress(Bitmap.CompressFormat.PNG,100, stream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally {
			if (stream != null) {
				try {
					stream.close();
					if(flag){
						img.setImageBitmap(decodeFile(filepath+"/ticket.png"));
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return file;
	}

	/**
	 * 根据 路径 得到 file 得到 bitmap
	 * @param filePath
	 * @return
	 * @throws IOException
	 */
	public static Bitmap decodeFile(String filePath) throws IOException{
		Bitmap b = null;
		int IMAGE_MAX_SIZE = 600;

		File f = new File(filePath);
		if (f == null){
			return null;
		}
		//Decode image size
		BitmapFactory.Options o = new BitmapFactory.Options();
		o.inJustDecodeBounds = true;

		FileInputStream fis = new FileInputStream(f);
		BitmapFactory.decodeStream(fis, null, o);
		fis.close();

		int scale = 1;
		if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
			scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
		}

		//Decode with inSampleSize
		BitmapFactory.Options o2 = new BitmapFactory.Options();
		o2.inSampleSize = scale;
		fis = new FileInputStream(f);
		b = BitmapFactory.decodeStream(fis, null, o2);
		fis.close();
		return b;
	}

	/**
	 * 创建二维码
	 *
	 * @param content   content
	 * @param widthPix  widthPix
	 * @param heightPix heightPix
	 * @param logoBm    logoBm
	 * @return 二维码
	 */
	public static Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm) {
		return null;
	}

	/**
	 * 在二维码中间添加Logo图案
	 */
	private static Bitmap addLogo(Bitmap src, Bitmap logo) {
		if (src == null) {
			return null;
		}
		if (logo == null) {
			return src;
		}
		//获取图片的宽高
		int srcWidth = src.getWidth();
		int srcHeight = src.getHeight();
		int logoWidth = logo.getWidth();
		int logoHeight = logo.getHeight();
		if (srcWidth == 0 || srcHeight == 0) {
			return null;
		}
		if (logoWidth == 0 || logoHeight == 0) {
			return src;
		}
		//logo大小为二维码整体大小的1/5
		float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
		Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
		try {
			Canvas canvas = new Canvas(bitmap);
			canvas.drawBitmap(src, 0, 0, null);
			canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
			canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);
			canvas.restore();
		} catch (Exception e) {
			bitmap = null;
			e.getStackTrace();
		}
		return bitmap;
	}
}
